//
//  MYEmoticonViewCell.swift
//  NewMicroblog
//
//  Created by apple on 2016/11/4.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class MYEmoticonViewCell: UICollectionViewCell {

    fileprivate lazy var emoticonButtonArray: [UIButton] = [UIButton]()


    var indexPath: IndexPath?{
        didSet{
            guard let idx = indexPath else {
                return
            }
            messageLabel.text = "当前显示的第\(idx.section + 1)组第\(idx.item + 1)页"
        }
    }

    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel(title: "", fontSize: 30, textColor: UIColor.white)
        return label
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    ///搭建主界面
    private func setupUI(){

        addChildButton()
        contentView.addSubview(messageLabel)
        messageLabel.snp_makeConstraints { (make) in
            make.center.equalTo(contentView)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func addChildButton(){
        for _ in 0..<20{
            let button = UIButton()
            button.backgroundColor = MYRandomColor()
            contentView.addSubview(button)
            emoticonButtonArray.append(button)
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        let btnW = width / 7
        let btnH = height / 3
        for (i, button) in emoticonButtonArray.enumerated() {
            let colIndex = i % 7
            let rowIndex = i / 7
            button.x = CGFloat(colIndex) * btnW
            button.y = CGFloat(rowIndex) * btnH
            button.size = CGSize(width: btnW, height: btnH)
        }

    }

}
